package com.xhh.nphm.gaoxin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xhh.nphm.common.annotation.DataSource;
import com.xhh.nphm.common.annotation.RepeatSubmit;
import com.xhh.nphm.common.constant.Constants;
import com.xhh.nphm.common.core.controller.BaseController;
import com.xhh.nphm.common.core.domain.AjaxResult;
import com.xhh.nphm.common.core.domain.QueryConditionVo;
import com.xhh.nphm.common.core.domain.entity.SysUser;
import com.xhh.nphm.common.core.page.TableDataInfo;
import com.xhh.nphm.common.utils.DateUtils;
import com.xhh.nphm.common.utils.ShiroUtils;
import com.xhh.nphm.common.utils.poi.ExcelUtil;
import com.xhh.nphm.gaoxin.entity.GxqTwfSendBackEty;
import com.xhh.nphm.gaoxin.entity.GxqTworkFormBaseEty;
import com.xhh.nphm.gaoxin.entity.GxqTworkFormLogEty;
import com.xhh.nphm.gaoxin.service.GxqITworkFormLogService;
import com.xhh.nphm.gaoxin.service.GxqIWfSendBackService;
import com.xhh.nphm.gaoxin.service.GxqIWorkFormBaseService;
import com.xhh.nphm.system.domain.*;
import com.xhh.nphm.system.mapper.CommonDao;
import com.xhh.nphm.system.mapper.SortMapper;
import com.xhh.nphm.system.service.*;
import com.xhh.nphm.web.controller.common.CommonController;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/gaoxin/system/mTelWF")
public class GxqMTelWFcmnFileController extends BaseController {
    private String prefix = "system/gaoxin/mTelWF";
    private String prefix1 = "system/gaoxin/mTelWF/rCity";
    @Autowired
    private CommonDao commonDao;
    @Autowired
    private SortMapper sortMapper;
    @Autowired
    private ITagService tagService;
    @Autowired
    private CommonController commonController;
    @Autowired
    private IsGoBackConfService isGoBackConfService;
    @Autowired
    private ISprocessStepService sprocessStepService;
    @Autowired
    private IReplyContentService replyContentService;
    @Autowired
    private IReplyTemplateService replyTemplateService;
    @Autowired
    private GetAWaitReplyTelService getAWaitReplyTelService;
    @Autowired
    private GxqIWfSendBackService gxqWfSendBackService;
    @Autowired
    private GxqIWorkFormBaseService gxqWorkFormBaseService;
    @Autowired
    private GxqITworkFormLogService gxqITworkFormLogService;


    /**
     * 调整承办部门 列表页面
     *
     * @param
     * @return
     */
    @GetMapping("/cmnFile/sendBackList/{id}")
    public ModelAndView sendBackList(@PathVariable("id") Integer pkId, ModelMap modelMap) {
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.selectByPkId(pkId);
        modelMap.put("wfId", pkId);
        modelMap.put("getDealPK", GxqTworkFormBaseEty.getGetDealPk());
        return new ModelAndView(prefix + "/cmnFile/sendBackList");
    }

    /**
     * 调整承办部门 列表数据
     *
     * @return
     */
    @PostMapping("/cmnFile/sendBackListJson")
    public TableDataInfo sendBackListJson(GxqTwfSendBackEty GxqTwfSendBackEty) {
        startPage();
        List<GxqTwfSendBackEty> list = gxqWfSendBackService.sendBackListJson(GxqTwfSendBackEty);
        return getDataTable(list);
    }

    @RequestMapping("/cmnFile/remove")
    public AjaxResult removeCmn(String... ids) {
        return toAjax(gxqWfSendBackService.deleteByIds(ids));
    }

    /**
     * 撤回承办单位处理中的工单
     *
     * @param
     * @return 响应结果
     */
    @PostMapping("/cmnFile/flowRollBack")
    public AjaxResult flowRollBack(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        //获取工单当前信息
        UpdateWrapper<GxqTworkFormBaseEty> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PkId", GxqTworkFormBaseEty.getPkId());
        updateWrapper.set("CrtStepId", 120);
        updateWrapper.set("CrtStepName", "再次整理");
        updateWrapper.set("CrtStepTime", DateUtils.getNowDate());
        boolean flag = gxqWorkFormBaseService.update(null, updateWrapper);
        if (flag) {
            //记录工单操作日志
            SysUser user = ShiroUtils.getSysUser();
            String desc = "调整办理部门：";
            GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
            etyLog.setWfId(GxqTworkFormBaseEty.getPkId());
            etyLog.setOpDeptNo(user.getDeptId().toString());
            etyLog.setOpDeptName(user.getDeptName());
            etyLog.setOpUserNo(user.getUserNo());
            etyLog.setOpUserName(user.getUserName());
            etyLog.setOpBtnId(-1);
            etyLog.setOpAction("调整");
            etyLog.setWsDesc(desc);
            etyLog.setWsOpen(1);
            etyLog.setEffectFlag(1);
            etyLog.setOpDesc(desc);
            gxqITworkFormLogService.save(etyLog);
        }
        return toAjax(flag);
    }

    /**
     * GxqTwfSendBackEty电话工单审核 列表页面
     *
     * @param
     * @return
     */
    @GetMapping("/cmnFile/treturnFlow")
    public ModelAndView treturnFlow() {
        return new ModelAndView(prefix + "/cmnFile/treturnFlow");
    }

    /**
     * 新增办理情况页面
     *
     * @return
     */
    @GetMapping("/cmnFile/add/{id}")
    public ModelAndView addCmnFile(@PathVariable("id") Integer pkId, ModelMap map) {
        SysUser user = ShiroUtils.getSysUser();
        Long deptId = user.getDeptId();
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.getById(pkId);
        GxqTwfSendBackEty sendBackEty = GxqTworkFormBaseEty.getTwfSendBackEty();
        sendBackEty.setSdeptId(deptId.toString());
        sendBackEty.setSdeptName(user.getDeptName());
        sendBackEty.setSuserNo(user.getUserNo());
        sendBackEty.setSuserName(user.getUserName());
        map.put("workFormBase", GxqTworkFormBaseEty);
        map.put("wfId", GxqTworkFormBaseEty.getPkId());
        map.put("sdeptName", user.getDept().getDeptShort());
        map.put("sdeptId", sendBackEty.getSdeptId());
        map.put("suserName", sendBackEty.getSuserName());
        map.put("suserNo", sendBackEty.getSuserNo());
        map.put("deptId", user.getDeptId());
        map.put("sendTime", DateUtils.getTime());
        return new ModelAndView(prefix + "/cmnFile/add");
    }


    /**
     * 办理情况编辑页面
     *
     * @param
     * @return
     */
    @GetMapping("/cmnFile/edit/{pkId}")
    public ModelAndView twfDeferEdit(@PathVariable Integer pkId, ModelMap map) {
        GxqTwfSendBackEty sendBackEty = gxqWfSendBackService.getById(pkId);
        map.put("sendBackEty", sendBackEty);
        map.put("workFromBase", gxqWorkFormBaseService.selectByPkId(sendBackEty.getWfId()));
        return new ModelAndView(prefix + "/cmnFile/edit");
    }

    @RequestMapping("/cmnFile/edit")
    @RepeatSubmit
    public AjaxResult editCmn(GxqTwfSendBackEty sendBackEty) {
        return toAjax(gxqWfSendBackService.updateSendBack(sendBackEty));
    }


    /**
     * 下级退回电话工单审核 列表数据
     *
     * @return
     */
    @PostMapping("/cmnFile/treturnFlowJson")
    public TableDataInfo treturnFlowJson(GxqTwfSendBackEty GxqTwfSendBackEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<GxqTwfSendBackEty> list = gxqWfSendBackService.treturnFlowJson(GxqTwfSendBackEty, user.getDeptId().toString());
        return getDataTable(list);
    }

    /**
     * 根据id 删除 下级退回工单审核
     *
     * @param ids
     * @return
     */
    @PostMapping("/cmnFile/remove")
    public AjaxResult remove(String... ids) {
        return toAjax(gxqWfSendBackService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 进入待本部门回复的列表页面
     */
    @GetMapping("/cmnFile/muReplyWait")
    public ModelAndView muReplyWait(ModelMap map) {
        SysUser sysUser = getSysUser();
        map.put("sysUser", sysUser);
        return new ModelAndView(prefix + "/cmnFile/muReplyWait");
    }

    /**
     * 加载待本部门回复的列表数据
     */
    @PostMapping("/cmnFile/muReplyWaitJson")
    public TableDataInfo muReplyWaitJson(QueryConditionVo qc) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = gxqWfSendBackService.muReplyWaitJson(user.getDeptId(),qc);
        return getDataTable(list);
    }

    /**
     * 加载待本部门回复的列表数据
     * 高新区
     */
    @PostMapping("/cmnFile/muReplyWaitJson_gxq")
    public TableDataInfo muReplyWaitJson_gxq(QueryConditionVo qc) {
        Long deptId = getSysUser().getDeptId();
        int count = gxqWfSendBackService.dueWorkOrderCount(deptId.toString());
        HashMap<String, Object> dueCount = new HashMap<>();
        dueCount.put("dueCount",count);
        startPage();
        List<Map<String, Object>> list = gxqWfSendBackService.muReplyWaitJson_gxq(deptId,qc);
        for (Map<String, Object> map : list) {
            map.putAll(dueCount);
        }
        return getDataTable(list);
    }

    /**
     * 市级未签收 列表页面
     *
     * @return
     */
    @GetMapping("/cmnFile/muSignNot")
    public ModelAndView muSignNot() {
        return new ModelAndView(prefix + "/cmnFile/muSignNot");
    }

    /**
     * 市级未签收列表数据
     */
    @PostMapping("/cmnFile/muSignNotJson")
    public TableDataInfo muSignNotJson(GxqTwfSendBackEty GxqTwfSendBackEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<GxqTwfSendBackEty> list;
        if(IS_GAOXIN()){
            list = gxqWfSendBackService.muSignNotJson_gxq(GxqTwfSendBackEty, user.getDeptId().toString());
        }else{
            list = gxqWfSendBackService.muSignNotJson(GxqTwfSendBackEty, user.getDeptId().toString());
        }
        return getDataTable(list);
    }

    /**
     * 市级未签收 处理页面
     */
    @GetMapping("/cmnFile/showWorkForm/{pkId}")
    public ModelAndView showWorkForm(@PathVariable Integer pkId, ModelMap map) {
        SysUser user = getSysUser();
        GxqTwfSendBackEty GxqTwfSendBackEty = gxqWfSendBackService.getById(pkId);
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.getById(GxqTwfSendBackEty.getWfId());
        List<Map<String, Object>> telExistTags = null;
        if (GxqTwfSendBackEty != null) {
            map.put("pkId", GxqTwfSendBackEty.getPkId());
            map.put("sortCode", GxqTwfSendBackEty.getSortCode());
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            map.put("backLimit", format.format(GxqTwfSendBackEty.getBackLimit()));
            map.put("sendBack", GxqTwfSendBackEty);
            map.put("isGoBackToPeerDept", isGoBackConfService.checkPeerDeptIsGoBack(GxqTwfSendBackEty.getSdeptId(),GxqTwfSendBackEty.getSbdeptId()));
        }
        if (GxqTworkFormBaseEty != null) {
            commonController.hidetTel_gxq(user,GxqTworkFormBaseEty);
            map.put("workFormBase", GxqTworkFormBaseEty);
            map.put("wfId", GxqTworkFormBaseEty.getPkId());
            // 查询工单已有标签
            telExistTags = tagService.selectTelExistTag(String.valueOf(GxqTworkFormBaseEty.getGetDealPk()),user.getDeptId().toString(),user.getUserId().toString());
        }
        if(telExistTags != null && telExistTags.size() > 0){
            map.put("wfTag",telExistTags);
        }
        map.put("user", user);
        //内容类别 完整展示三级分类
        String dcntAnameAll = getDcntAnameAll(GxqTworkFormBaseEty);
        map.put("dcntAnameAll",dcntAnameAll);
        return new ModelAndView(prefix + "/cmnFile/showWorkForm");
    }

    /**
     * 获取内容类别（三级完整展示）
     */
    public String getDcntAnameAll(GxqTworkFormBaseEty GxqTworkFormBaseEty){
        List list =new ArrayList();
        String dcntAnameAll="";
        if(GxqTworkFormBaseEty!=null){
            String dcntAname = GxqTworkFormBaseEty.getDcntAname();
            String dcntAcode = GxqTworkFormBaseEty.getDcntAcode();
            if (dcntAcode!=null&&!"".equals(dcntAcode)){
                if (dcntAname!=null){
                    list.add(dcntAname);
                }
                BSort bSort = sortMapper.selectByCodePk(dcntAcode);
                if(bSort!=null){
                    String codeparent = bSort.getCodeparent();
                    for(int j=0;!"11".equals(codeparent)&&j<2;j++){
                        BSort bSort1 = sortMapper.selectByCodePk(codeparent);
                        if(bSort1!=null){
                            list.add(bSort1.getSortname());
                            codeparent=bSort1.getCodeparent();
                        }else {
                            break;
                        }
                    }
                }
            }
            int size = list.size();
            if (size>0){
                for (int i=size-1;i>=0;i--){
                    dcntAnameAll=dcntAnameAll+list.get(i)+" ";
                }
            }

        }
        return dcntAnameAll;
    }

    /**
     * 市级未签收 提交下一步 数据
     */
    @PostMapping("/cmnFile/showWorkFormNextJson")
    public AjaxResult showWorkFormJson(String remark, GxqTwfSendBackEty GxqTwfSendBackEty, GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        //设置本级签收备注
        GxqTwfSendBackEty.setBackRemark(remark == null ? "":remark);
        SysUser user = ShiroUtils.getSysUser();
        Date date = new Date();
        boolean flag = false;
        GxqTwfSendBackEty twfSendBackOld = gxqWfSendBackService.getById(GxqTwfSendBackEty.getPkId());
        int sendStateOld = twfSendBackOld.getSendState();
        if("21,22".indexOf(String.valueOf(sendStateOld)) == -1) {
            GxqTworkFormBaseEty tworkFormBase = gxqWorkFormBaseService.getById(GxqTwfSendBackEty.getWfId());
            // 更新工单
            UpdateWrapper<GxqTworkFormBaseEty> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("PkId", tworkFormBase.getPkId());
            updateWrapper.set("WsUFlag", 1);
            updateWrapper.set("CrtStepID", GxqTworkFormBaseEty.getCrtStepId());
            updateWrapper.set("CrtStepName", GxqTworkFormBaseEty.getCrtStepName());
            updateWrapper.set("CrtStepTime", date);
            gxqWorkFormBaseService.update(null, updateWrapper);
            // 更新派单
            UpdateWrapper<GxqTwfSendBackEty> wrapper = new UpdateWrapper<>();
            wrapper.eq("PkId", GxqTwfSendBackEty.getPkId());
            wrapper.set("SendState", GxqTwfSendBackEty.getSendState());
            wrapper.set("GetTime", date);
            wrapper.set("PromisesTime", GxqTwfSendBackEty.getPromisesTime());
            wrapper.set("BackState", 0);
            wrapper.set("GetPerson", user.getUserNo());
            wrapper.set("GetPerTel", user.getPhonenumber());
            wrapper.set("BackRemark", GxqTwfSendBackEty.getBackRemark());
            flag = gxqWfSendBackService.update(null, wrapper);
            // 派单更新成功 新增日志
            if (flag) {
                GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
                etyLog.setWfId(GxqTwfSendBackEty.getWfId()); //[tWorkFormBase]PkId
                etyLog.setOpDeptNo(user.getDeptId().toString());
                etyLog.setOpDeptName(user.getDeptName());
                etyLog.setOpUserNo(user.getUserNo());
                etyLog.setOpUserName(user.getUserName());
                etyLog.setOpBtnId(-1);
                etyLog.setOpAction("签收");
                etyLog.setWsDesc("签收工单。"); //Show In website
                if ("21".equals(GxqTwfSendBackEty.getSortCode())) {
                    etyLog.setWsOpen(0); //website ShowFlag
                    etyLog.setEffectFlag(0);
                } else {
                    etyLog.setWsOpen(1); //website ShowFlag
                    etyLog.setEffectFlag(1);
                }
                String signDesc = GxqTwfSendBackEty.getSendRemark() == null ? "" : GxqTwfSendBackEty.getSendRemark();
                if (GxqTwfSendBackEty.getSendState() == 22) {
                    signDesc = signDesc == null || signDesc.equals("") ? "后续转下级办理" : "后续转下级办理（" + signDesc + "）";
                } else {
                    signDesc = signDesc == null || signDesc.equals("") ? "后续本单位办理" : "后续本单位办理（" + signDesc + "）";
                }
                etyLog.setOpDesc(signDesc);
                flag = gxqITworkFormLogService.save(etyLog) > 0;
            }
        } else {
            // 重复签收和待下派 不做任何操作
            flag = true;
        }
        return toAjax(flag);
    }

    /**
     * 市级未签收 退回数据
     */
    @PostMapping("/cmnFile/showWorkFormBackJson")
    public AjaxResult showWorkFormBackJson(GxqTwfSendBackEty GxqTwfSendBackEty) {
        SysUser user = ShiroUtils.getSysUser();
        if (Objects.equals(user.getAreaNo(), "510109")) {
            return showWorkFormBackJson_gxq(GxqTwfSendBackEty);
        }else{
            return showWorkFormBackJson_general(GxqTwfSendBackEty);
        }
    }

    public AjaxResult showWorkFormBackJson_general(GxqTwfSendBackEty GxqTwfSendBackEty) {
        SysUser user = ShiroUtils.getSysUser();
        UpdateWrapper<GxqTwfSendBackEty> wrapper = new UpdateWrapper<>();
        wrapper.eq("PkId", GxqTwfSendBackEty.getPkId());
        wrapper.set("SendState", 40);
        wrapper.set("GetTime", new Date());
        wrapper.set("BackState", 11);
        wrapper.set("GetPerson", user.getUserNo());
        wrapper.set("GetPerTel", user.getPhonenumber());
        wrapper.set("BackRemark", GxqTwfSendBackEty.getBackRemark());
        wrapper.set("FlagInt", 1);
        wrapper.set("SendRemark", "");
        boolean flag = gxqWfSendBackService.update(null, wrapper);
        if (flag) {
            GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
            etyLog.setWfId(GxqTwfSendBackEty.getWfId()); //[tWorkFormBase]PkId
            etyLog.setOpDeptNo(user.getDeptId().toString());
            etyLog.setOpDeptName(user.getDeptName());
            etyLog.setOpUserNo(user.getUserNo());
            etyLog.setOpUserName(user.getUserName());

            etyLog.setOpBtnId(-1);
            etyLog.setOpAction("退回");
            etyLog.setWsDesc("退回工单。"); //Show In website
            if ("21".equals(GxqTwfSendBackEty.getSortCode())) {
                etyLog.setWsOpen(0); //website ShowFlag
                etyLog.setEffectFlag(0);
            } else {
                etyLog.setWsOpen(1); //website ShowFlag
                etyLog.setEffectFlag(1);
            }
            etyLog.setOpDesc("退回: " + GxqTwfSendBackEty.getSdeptName() + "（" + GxqTwfSendBackEty.getBackRemark() + "）");
            flag = gxqITworkFormLogService.save(etyLog) > 0;
        }
        //更新父级派单状态
        commonDao.proTsubBackState(GxqTwfSendBackEty.getWfId(),GxqTwfSendBackEty.getPkId());
        return toAjax(flag);
    }

    public AjaxResult showWorkFormBackJson_gxq(GxqTwfSendBackEty GxqTwfSendBackEty) {
        SysUser user = ShiroUtils.getSysUser();
        UpdateWrapper<GxqTwfSendBackEty> wrapper = new UpdateWrapper<>();
        wrapper.eq("PkId", GxqTwfSendBackEty.getPkId());
        wrapper.set("SendState", 50);
        wrapper.set("GetTime", new Date());
        wrapper.set("BackState", 11);
        wrapper.set("GetPerson", user.getUserNo());
        wrapper.set("GetPerTel", user.getPhonenumber());
        wrapper.set("BackRemark", GxqTwfSendBackEty.getBackRemark());
        wrapper.set("FlagInt", 1);//未审核
        wrapper.set("SendRemark", "");
        boolean flag = gxqWfSendBackService.update(null, wrapper);
        if (flag) {
            GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
            etyLog.setWfId(GxqTwfSendBackEty.getWfId()); //[tWorkFormBase]PkId
            etyLog.setOpDeptNo(user.getDeptId().toString());
            etyLog.setOpDeptName(user.getDeptName());
            etyLog.setOpUserNo(user.getUserNo());
            etyLog.setOpUserName(user.getUserName());
            etyLog.setOpBtnId(-1);
            etyLog.setOpAction("退回本级");
            etyLog.setWsDesc("退回本级工单。"); //Show In website
            if ("21".equals(GxqTwfSendBackEty.getSortCode())) {
                etyLog.setWsOpen(0); //website ShowFlag
                etyLog.setEffectFlag(0);
            } else {
                etyLog.setWsOpen(1); //website ShowFlag
                etyLog.setEffectFlag(1);
            }
            etyLog.setOpDesc("退回本级: " + user.getDeptName() + "（" + GxqTwfSendBackEty.getBackRemark() + "）");
            flag = gxqITworkFormLogService.save(etyLog) > 0;
        }
        //更新父级派单状态
        commonDao.proTsubBackState(GxqTwfSendBackEty.getWfId(),GxqTwfSendBackEty.getPkId());
        return toAjax(flag);
    }

    /**
     * 拟派下级派单页面
     *
     * @return
     */
    @GetMapping("/cmnFile/dlSendToSon/{wfId}")
    public ModelAndView dlSendToSon(@PathVariable Integer wfId,Integer sbId, ModelMap map) {
        SysUser user = ShiroUtils.getSysUser();
        Long deptId = user.getDeptId();
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.getById(wfId);
        GxqTwfSendBackEty sendBackEty = GxqTworkFormBaseEty.getTwfSendBackEty();
        // 查询工单已有标签
        List<Map<String, Object>> telExistTags = tagService.selectTelExistTag(String.valueOf(GxqTworkFormBaseEty.getGetDealPk()),user.getDeptId().toString(),user.getUserId().toString());
        if(telExistTags != null && telExistTags.size() > 0){
            map.put("wfTag",telExistTags);
        }
        sendBackEty.setSdeptId(deptId.toString());
        sendBackEty.setSdeptName(user.getDeptName());
        sendBackEty.setSuserNo(user.getUserNo());
        sendBackEty.setSuserName(user.getUserName());
        //TODO 设置办理期限 不是二级部门（锦江区），把派单的办理期限设置为父级派单办理期限
        if(sbId != null) {
            GxqTwfSendBackEty psendBack = gxqWfSendBackService.selectByPkId(sbId);
            map.put("pbackLimit", psendBack.getBackLimit());
        }
        else {
            QueryWrapper<GxqTwfSendBackEty> queryWrapper = new QueryWrapper<GxqTwfSendBackEty>();
            queryWrapper.eq("wfId", wfId);
            queryWrapper.eq("sbdeptId", deptId.toString());
            queryWrapper.eq("isDel", 0);
            List<GxqTwfSendBackEty> list = gxqWfSendBackService.list(queryWrapper);
            if(list != null && list.size() > 0) {
                map.put("pbackLimit", list.get(0).getBackLimit());
            }
        }
        map.put("workFormBase", GxqTworkFormBaseEty);
        map.put("sbId", sbId);
        map.put("wfId", GxqTworkFormBaseEty.getPkId());
        map.put("telId", GxqTworkFormBaseEty.getGetDealPk());
        map.put("sdeptName", user.getDept().getDeptShort());
        map.put("sdeptId", sendBackEty.getSdeptId());
        map.put("suserName", sendBackEty.getSuserName());
        map.put("suserNo", sendBackEty.getSuserNo());
        map.put("deptId", user.getDeptId());
        map.put("sendTime", DateUtils.getTime());
        return new ModelAndView(prefix + "/cmnFile/dlSendToSon");
    }

    /**
     * 承办部门选择
     *
     * @return
     */
    @GetMapping("/cmnFile/deptAllM")
    public ModelAndView deptAllM(ModelMap map) {
        map.put("areaNo", getSysUser().getAreaNo());
        map.put("deptId", getSysUser().getDeptId());
        return new ModelAndView(prefix + "/cmnFile/deptAllM");
    }

    /**
     * 进入本部门电话工单回复页面
     */
    @GetMapping("/cmnFile/dlReplyS")
    public ModelAndView dlReplyS(Integer sendId,Integer wfId, ModelMap map) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SysUser user = getSysUser();
        map.put("user", user);
        map.put("sendId", sendId);
        map.put("nowTime", sdf.format(DateUtils.getNowDate()));
        map.put("workForm",gxqWorkFormBaseService.getById(wfId));
        map.put("sendBackEty", gxqWfSendBackService.getById(sendId));
        List<GxqTwfSendBackEty> subDeptHandleWF = gxqWfSendBackService.findSubDeptHandleWF(wfId, user.getDeptId());
        //两条及其以上时过滤掉groupIndex>1的数据
        if (subDeptHandleWF.size() > 1){
            subDeptHandleWF = subDeptHandleWF.stream().filter(val -> val.getGroupIndex() == 1).collect(Collectors.toList());
        }
        map.put("sonBackList", subDeptHandleWF);
        map.put("replyTemplate",replyTemplateService.selectTelRnByDeptOrUser(user.getDeptId().toString(), user.getUserNo()));
        return new ModelAndView(prefix + "/cmnFile/dlReplyS");
    }

    /**
     * 回复电话工单功能
     *
     * @return 响应结果
     */
    @PostMapping("/cmnFile/dlReplyS")
    public AjaxResult dlReplyS(GxqTwfSendBackEty sendBackEty, GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        //保存响应结果
        boolean flag = false;
        String oldStr = sendBackEty.getBackDesc();
        if(oldStr != null){
            sendBackEty.setBackDesc(oldStr.replace(removeStr,""));
        }
        //修改派单表数据
        if (!Objects.isNull(sendBackEty)) {
            UpdateWrapper<GxqTwfSendBackEty> updateWrapper = new UpdateWrapper<>();
            updateWrapper
                    .set("BackState", 10)
                    .set("BDutyDept", sendBackEty.getBdutyDept())
                    .set("BDutyTel", sendBackEty.getBdutyTel())
                    .set("BUserNo", sendBackEty.getBuserNo())
                    .set("BUserName", sendBackEty.getBuserName())
                    .set("BackRemark", sendBackEty.getBackRemark())
                    .set("BackDesc", sendBackEty.getBackDesc())
                    .set("BackTime", sendBackEty.getBackTime())
                    .set("BAttachPath", "")
                    .set("BAttachName", "")
                    .set("Reason", sendBackEty.getReason())
                    .eq("PkId", sendBackEty.getPkId());
            flag = gxqWfSendBackService.update(updateWrapper);
            //更新父级派单状态
            commonDao.proTsubBackState(sendBackEty.getWfId(),sendBackEty.getPkId());
            //判断工单所有的派单部门都已办结完毕，完毕则修改工单状态
            if (!"21".equals(sendBackEty.getSortCode())) {
                List tmpLst = gxqWfSendBackService.ltSendBackMain(sendBackEty.getWfId(), sendBackEty.getSdeptId());
                int myCounter = 0;
                for (int i = 0; i < tmpLst.size(); i++) {
                    GxqTwfSendBackEty tmpEty = (GxqTwfSendBackEty) tmpLst.get(i);
                    if (tmpEty.getBackState() > 0) {
                        myCounter++;
                    }
                }
                myCounter = tmpLst.size() - myCounter;
                if (myCounter == 0) {
                    //修改工单状态
                    UpdateWrapper<GxqTworkFormBaseEty> etyUpdateWrapper = new UpdateWrapper<>();
                    etyUpdateWrapper
                            .set("CrtStepID", GxqTworkFormBaseEty.getCrtStepId())
                            .set("CrtStepName", GxqTworkFormBaseEty.getCrtStepName())
                            .set("CrtStepTime", GxqTworkFormBaseEty.getCrtStepTime())
                            .eq("PkId", sendBackEty.getWfId());
                    flag = gxqWorkFormBaseService.update(etyUpdateWrapper);
                }
            }
            //新增工单操作记录
            String desc = "回复：" + sendBackEty.getSdeptName();
            GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
            SysUser user = ShiroUtils.getSysUser();
            etyLog.setWfId(sendBackEty.getWfId());
            etyLog.setOpDeptNo(user.getDeptId().toString());
            etyLog.setOpDeptName(user.getDeptName());
            etyLog.setOpUserNo(user.getUserNo());
            etyLog.setOpUserName(user.getUserName());
            etyLog.setOpBtnId(-1);
            etyLog.setOpAction("回复");
            etyLog.setWsDesc("提交办理结果。");
            if ("21".equals(sendBackEty.getSortCode())) {
                etyLog.setWsOpen(0);
                etyLog.setEffectFlag(0);
            } else {
                etyLog.setWsOpen(1);
                etyLog.setEffectFlag(1);
            }
            etyLog.setOpDesc(desc);
            flag = gxqITworkFormLogService.save(etyLog) > 0;
        }
        return toAjax(flag);
    }

    /**
     * 待本部门回复-退回电话工单页面
     */
    @GetMapping("/cmnFile/dlBackS")
    public ModelAndView dlBackS(Integer wfId, Integer sendId, ModelMap map) {
        SysUser sysUser = ShiroUtils.getSysUser();
        map.put("wfId", wfId);
        map.put("sendId", sendId);
        map.put("workFormEty", gxqWorkFormBaseService.getById(wfId));
        GxqTwfSendBackEty sendBackEty = gxqWfSendBackService.getById(sendId);
        map.put("sendBackEty", sendBackEty);
        map.put("isGoBackToPeerDept",isGoBackConfService.checkPeerDeptIsGoBack(sendBackEty.getSdeptId(),sendBackEty.getSbdeptId()));
        map.put("user", sysUser);
        return new ModelAndView(prefix + "/cmnFile/dlBackS");
    }

    /**
     * 待本部门回复-退回电话工单
     *
     * @return 响应结果
     */
    @PostMapping("/cmnFile/dlBackS")
    public AjaxResult dlBackS(GxqTwfSendBackEty sendBackEty, GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        //保存响应结果
        boolean flag = false;
        SysUser user = ShiroUtils.getSysUser();
        //修改派单表数据
        UpdateWrapper<GxqTwfSendBackEty> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("SendState", 40)
                .set("GetTime", DateUtils.getNowDate())
                .set("GetPerson", user.getUserNo())
                .set("GetPerTel", user.getTklTNo())
                .set("BackRemark", sendBackEty.getBackRemark())
                .set("BackState", 11)
                .set("FlagInt", 1)
                .eq("PkId", sendBackEty.getPkId());
        flag = gxqWfSendBackService.update(updateWrapper);
        //新增工单操作记录
        //生成工单操作日志
        String backRemark = sendBackEty.getBackRemark() == null ? "无" : sendBackEty.getBackRemark();
        HashMap<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("wfId", sendBackEty.getWfId());
        map.put("sortCode", sendBackEty.getSortCode());
        map.put("opBtnId", -1);
        map.put("opAction", "退回");
        map.put("wsDesc", "退回工单");
        map.put("opDesc", "退回:" + sendBackEty.getSdeptName() + "(" + backRemark + ")");
        GxqTworkFormLogEty etyLog = generateGxqTworkFormLogEty(map);
        flag = gxqITworkFormLogService.save(etyLog) > 0;
        return toAjax(flag);
    }

    /**
     * 承办单位处理 撤回页面
     */
    @GetMapping("/cmnFile/flowRollBack/{pkId}")
    public ModelAndView flowRollBack(@PathVariable Integer pkId, ModelMap map) {
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.getById(pkId);
        SprocessStepEty stepEty = sprocessStepService.getByProcess(GxqTworkFormBaseEty.getSetProId(), GxqTworkFormBaseEty.getCrtStepId());
        if (GxqTworkFormBaseEty != null) {
            map.put("workFormBase", GxqTworkFormBaseEty);
        }
        if (stepEty != null) {
            map.put("preName", stepEty.getPreName());
            map.put("preId", stepEty.getPreId());
        }
        return new ModelAndView(prefix + "/cmnFile/flowRollBack");
    }

    /**
     * 承办单位处理中 最近操作记录 数据
     */
    @PostMapping("/cmnFile/flowRollBackLog/{wfId}")
    public TableDataInfo flowRollBackLog(@PathVariable Integer wfId) {
        startPage();
        List<GxqTworkFormLogEty> list = gxqITworkFormLogService.logListJson(wfId);
        return getDataTable(list);
    }

    /**
     * 进入审核本级回复页面
     */
    @GetMapping("/cmnFile/dlChkReply")
    public ModelAndView dlChkReply(Integer wfId, Integer sendId, ModelMap map) {
        map.put("sendBackEty", gxqWfSendBackService.selectByPkId(sendId));
        map.put("workFormEty", gxqWorkFormBaseService.selectByPkId(wfId));
        map.put("user", ShiroUtils.getSysUser());
        return new ModelAndView(prefix + "/cmnFile/dlChkReply");
    }

    /**
     * 审核本级回复
     */
    @PostMapping("/cmnFile/dlChkReply")
    public AjaxResult dlChkReply(GxqTwfSendBackEty sendBackEty, GxqTworkFormBaseEty workFormEty, HttpServletRequest req) {
        // 保存响应结果
        boolean flag = false;
        // 校验入参不为空
        if (!(Objects.isNull(sendBackEty) && Objects.isNull(workFormEty))) {
            // 获取当前登录用户
            SysUser user = ShiroUtils.getSysUser();
            // 获取审查意见
            String opinion = req.getParameter("opinion");
            // 获取不通过理由
            String reason = req.getParameter("txtReason");
            // fpAct
            int act = sendBackEty.getBackState();

            // 修改派单表
            flag = dlChkReplyUpdateSendBack(sendBackEty, opinion, reason);
            // 修改工单表
            // 判断是否要修改工单 [条件]用户部门、用户角色
            String userRole = user.getRoles().get(0).getRoleId() == null ? "" : user.getRoles().get(0).getRoleId().toString();
            flag = ((user.getDeptId().toString().length() == 3) ||
                    ("310".equals(userRole)) ||
                    ("340".equals(userRole))) &&
                    (!"21".equals(sendBackEty.getSortCode()));
            // 判断是否要修改工单 [条件]该派单工单性质
            if ((flag)) {
                flag = dlReplyUpdateWf(sendBackEty, workFormEty, act);
            }
            // 新增工单操作日志
            flag = dlReplyInsertWfLog(sendBackEty, user, opinion, reason, act);
        }
        return toAjax(flag);
    }

    /**
     * 审核本级回复-修改工单数据
     *
     * @param sendBackEty 派单数据
     * @param workFormEty 工单数据
     * @param act         通过与否
     * @return 结果
     */
    private boolean dlReplyUpdateWf(GxqTwfSendBackEty sendBackEty, GxqTworkFormBaseEty workFormEty, int act) {
        boolean flag = false;
        // 修改 条件构造器
        UpdateWrapper<GxqTworkFormBaseEty> wfbWrapper = new UpdateWrapper<>();
        // 查询 条件构造器
        QueryWrapper<GxqTwfSendBackEty> wfbQueryWrapper = new QueryWrapper<>();
        wfbQueryWrapper
                .eq("WfId", sendBackEty.getWfId())
                .eq("SDeptID", sendBackEty.getSdeptId())
                .ne("SortCode", "21")
                .ne("SendState", 40);
        List<GxqTwfSendBackEty> tmpList = gxqWfSendBackService.list(wfbQueryWrapper);
        // 查询 是否有 第二次派单 并且状态在 未审
        List<GxqTwfSendBackEty> list = tmpList.stream().filter(sendback -> sendback.getSeqCode().equals("20")).collect(Collectors.toList());
        int myCounter = 0;
        StringBuilder temp = new StringBuilder();
        if (list.size() > 0) {
            for (GxqTwfSendBackEty tmpEty : list) {
                switch (tmpEty.getBackState()) {
                    case 11:
                    case 21:
                    case 40:
                        myCounter++;
                        if (list.size() == 1) {
                            temp = spliceStr(tmpEty);
                            replyContentAdd(tmpEty, temp.toString());
                        } else {
                            QueryWrapper<ReplyContent> wrapper = new QueryWrapper<>();
                            wrapper.eq("wfId", tmpEty.getWfId()).eq("sendId", tmpEty.getPkId());
                            List<ReplyContent> replyContentList = replyContentService.list(wrapper);
                            if (replyContentList.size() < 1) {
                                temp = spliceStr(tmpEty);
                                replyContentAdd(tmpEty, temp.toString());
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        } else {
            for (GxqTwfSendBackEty tmpEty : tmpList) {
                switch (tmpEty.getBackState()) {
                    case 11:
                    case 21:
                    case 40:
                        myCounter++;
                        temp = spliceStr(tmpEty);
                        break;
                    default:
                        break;
                }
            }
        }

        if (list.size() > 0) {
            myCounter = list.size() - myCounter;
        } else {
            myCounter = tmpList.size() - myCounter;
        }
        if (myCounter == 0) {
            wfbWrapper
                    .set("WsUFlag", 1)
                    .set("CrtStepID", workFormEty.getCrtStepId())
                    .set("CrtStepName", workFormEty.getCrtStepName())
                    .set("CrtStepTime", workFormEty.getCrtStepTime())
                    .eq("PkId", sendBackEty.getWfId());
            if ((act == 11)) {
                wfbWrapper
                        .set("DOverTime", DateUtils.getNowDate())
                        .set("DOverDesc", temp.toString());
            }
            flag = gxqWorkFormBaseService.update(wfbWrapper);
        }
        return flag;
    }

    /**
     * 新增 二次回复内容
     */
    private void replyContentAdd(GxqTwfSendBackEty tmpEty, String doverDesc) {
        ReplyContent replyContent = new ReplyContent();
        replyContent.setWfId(tmpEty.getWfId());
        replyContent.setSendId(tmpEty.getPkId());
        replyContent.setReplyDesc(doverDesc);
        replyContent.setReplyDate(tmpEty.getBackTime());
        replyContent.setUserNo(tmpEty.getBuserNo());
        replyContent.setUserName(tmpEty.getBuserName());
        replyContentService.save(replyContent);
    }

    /**
     * 拼接 回复内容
     */
    private StringBuilder spliceStr(GxqTwfSendBackEty tmpEty) {
        StringBuilder temp = new StringBuilder();
        return temp.append(tmpEty.getSbdeptName())
                .append("(")
                .append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(tmpEty.getBackTime()))
                .append(")：\r\n")
                .append(tmpEty.getBackDesc())
                .append("\r\n\r\n");
    }

    /**
     * 审核本级回复-新增工单操作日志
     *
     * @param sendBackEty 派单数据
     * @param user        当前登录用户
     * @param opinion     审查意见
     * @param reason      不通过理由
     * @param act         通过：不通过
     * @return 结果
     */
    private boolean dlReplyInsertWfLog(GxqTwfSendBackEty sendBackEty, SysUser user, String opinion, String reason, int act) {
        boolean flag;
        GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
        etyLog.setWfId(sendBackEty.getWfId());
        etyLog.setOpDeptNo(user.getDeptId().toString());
        etyLog.setOpDeptName(user.getDeptName());
        etyLog.setOpUserNo(user.getUserNo());
        etyLog.setOpUserName(user.getUserName());
        etyLog.setOpBtnId(-1);
        StringBuilder temp = new StringBuilder();
        if (act == 11) {
            etyLog.setOpAction("通过");
            temp.append(opinion);
        } else {
            etyLog.setOpAction("不通过");
            temp.append("理由（").append(reason).append("）");
        }
        etyLog.setWsDesc(temp.toString());
        etyLog.setWsOpen(0);
        etyLog.setOpDesc(temp.toString());
        etyLog.setEffectFlag(0);
        flag = gxqITworkFormLogService.save(etyLog) > 0;
        return flag;
    }

    /**
     * 审核本级回复-修改派单数据
     *
     * @param sendBackEty 派单数据
     * @param opinion     审查意见
     * @param reason      不通过理由
     * @return 结果
     */
    private boolean dlChkReplyUpdateSendBack(GxqTwfSendBackEty sendBackEty, String opinion, String reason) {
        StringBuilder backRemark = new StringBuilder();
        backRemark.append(sendBackEty.getBackRemark() == null ? "" : sendBackEty.getBackRemark());
        backRemark.append("|");
        backRemark.append(opinion);
        backRemark.append("-");
        backRemark.append(reason);
        UpdateWrapper<GxqTwfSendBackEty> sbWrapper = new UpdateWrapper<>();
        sbWrapper
                .set("BackState", sendBackEty.getBackState())
                .set("BackRemark", backRemark.toString())
                .eq("PkId", sendBackEty.getPkId());
        sbWrapper.setSql("BackTime=getdate()");
        return gxqWfSendBackService.update(sbWrapper);
    }


    /**
     * 进入本部门已办毕列表页面
     */
    @GetMapping("/cmnFile/muReplyOver")
    public ModelAndView muReplyOver() {
        return new ModelAndView(prefix + "/cmnFile/muReplyOver");
    }

    /**
     * 加载本部门已办毕列表数据
     */
    @PostMapping("/cmnFile/muReplyOverJson")
    public TableDataInfo muReplyOverJson(@RequestParam("workFormNo") String workFormNo, @RequestParam("sdeptName") String sdeptName) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = gxqWfSendBackService.muReplyOverJson(user.getDeptId(), workFormNo, sdeptName);
        return getDataTable(list);
    }

    /**
     * 进入本部门已办毕工单编辑页面
     *
     * @param sendId 派单id
     */
    @GetMapping("/cmnFile/editReply")
    public ModelAndView editReply(Integer sendId, ModelMap map) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("user", ShiroUtils.getSysUser());
        map.put("nowTime", sdf.format(DateUtils.getNowDate()));
        map.put("sendBackEty", gxqWfSendBackService.getById(sendId));
        return new ModelAndView(prefix + "/cmnFile/editReply");
    }

    /**
     * 编辑本部门已办毕工单办理内容
     */
    @PostMapping("/cmnFile/editReply")
    public AjaxResult editReply(GxqTwfSendBackEty sendBackEty) {
        String oldStr = sendBackEty.getBackDesc();
        if(oldStr != null){
            sendBackEty.setBackDesc(oldStr.replace(removeStr,""));
        }
        //修改办理内容
        UpdateWrapper<GxqTwfSendBackEty> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("GetPerson", sendBackEty.getGetPerson())
                .set("BDutyTel", sendBackEty.getBdutyTel())
                .set("BDutyDept", sendBackEty.getBdutyDept())
                .set("BUserName", sendBackEty.getBuserName())
                .set("BackTime", sendBackEty.getBackTime())
                .set("BackDesc", sendBackEty.getBackDesc())
                .set("BackRemark", sendBackEty.getBackRemark())
                .eq("PkId", sendBackEty.getPkId());
        return toAjax(gxqWfSendBackService.update(updateWrapper));
    }

    /**
     * 进入审核本级回复列表页面
     */
    @GetMapping("/cmnFile/muReplyChk")
    public ModelAndView muReplyChk() {
        return new ModelAndView(prefix + "/cmnFile/muReplyChk");
    }

    /**
     * 加载审核本级回复的列表数据
     */
    @PostMapping("/cmnFile/muReplyChkJson")
    public TableDataInfo muReplyChkJson(@RequestParam("workFormNo") String workFormNo, @RequestParam("sdeptName") String sdeptName) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = gxqWfSendBackService.muReplyChkJson(user.getDeptId(), workFormNo, sdeptName);
        return getDataTable(list);
    }

    /**
     * 进入(区县)退回的工单T列表页面
     */
    @GetMapping("/cmnFile/muDeptBack")
    public ModelAndView muDeptBack() {
        return new ModelAndView(prefix + "/cmnFile/muDeptBack");
    }

    /**
     * 加载(区县)退回的工单T列表数据
     */
    @PostMapping("/cmnFile/muDeptBackJson")
    public TableDataInfo muDeptBackJson(@RequestParam("workFormNo") String workFormNo, @RequestParam("sdeptName") String sdeptName) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = gxqWfSendBackService.muDeptBackJson(user.getDeptId(), workFormNo, sdeptName);
        return getDataTable(list);
    }

    /**
     * 进入撤回已退回的工单页面
     *
     * @author zz
     */
    @GetMapping("/cmnFile/dlRegetS")
    public ModelAndView dlRegetS(Integer sendId, ModelMap map) {
        map.put("sendBackEty", gxqWfSendBackService.getById(sendId));
        map.put("user", getSysUser());
        return new ModelAndView(prefix + "/cmnFile/dlRegetS");
    }

    /**
     * 进入撤回待分发到下级工单页面
     *
     * @author sjk
     */
    @GetMapping("/cmnFile/revokeSendSubT")
    public ModelAndView revokeSendSubT(Integer sendId, ModelMap map) {
        map.put("sendBackEty", gxqWfSendBackService.getById(sendId));
        map.put("user", getSysUser());
        return new ModelAndView(prefix + "/cmnFile/revokeSendSubT");
    }

    /**
     * 撤回已退回工单
     *
     * @author zz
     */
    @PostMapping("/cmnFile/dlRegetS")
    public AjaxResult dlRegetS(GxqTwfSendBackEty sendBackEty,HttpServletRequest req) {
        //保存响应结果
        boolean flag = false;
        //获取当前登录用户信息
        SysUser user = getSysUser();
        //二级部门需修改工单内容
        if("210,240,260,290,310,340".contains(user.getRoles().get(0).getRoleId().toString())){
            //更新工单
            UpdateWrapper<GxqTworkFormBaseEty> wrap = new UpdateWrapper<>();
            wrap.eq("PkId", sendBackEty.getWfId());
            wrap.set("WsUFlag", 1);
            wrap.set("CrtStepID", req.getParameter("crtStepId"));
            wrap.set("CrtStepName", req.getParameter ("crtStepName"));
            wrap.set("CrtStepTime", DateUtils.getNowDate());
            gxqWorkFormBaseService.update(null, wrap);
        }
        //修改派单表记录
        UpdateWrapper<GxqTwfSendBackEty> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("SendState", sendBackEty.getSendState())
                .set("GetTime", DateUtils.getNowDate())
                .set("GetPerson", user.getUserNo())
                .set("GetPerTel", user.getPhonenumber())
                .set("BackRemark", sendBackEty.getBackRemark())
                .set("BackState", 0)
                .eq("pkId", sendBackEty.getPkId());
        flag = gxqWfSendBackService.update(updateWrapper);
        //生成工单操作日志
        String backRemark = sendBackEty.getBackRemark() == null ? "无" : sendBackEty.getBackRemark();
        HashMap<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("wfId", sendBackEty.getWfId());
        map.put("sortCode", sendBackEty.getSortCode());
        map.put("opBtnId", -1);
        map.put("opAction", "撤销退回(重新签收)");
        map.put("wsDesc", "撤销退回");
        map.put("opDesc", "撤销退回:" + sendBackEty.getSdeptName() + "(" + backRemark + ")");
        GxqTworkFormLogEty etyLog = generateGxqTworkFormLogEty(map);
        flag = gxqITworkFormLogService.save(etyLog) > 0;
        //设置父派单状态
        commonDao.proTsubBackState(sendBackEty.getWfId(),sendBackEty.getPkId());
        return toAjax(flag);
    }

    /**
     * 生成工单操作日志
     *
     * @param map 工单操作日志实体的属性
     * @return 工单操作日志实体
     */
    private GxqTworkFormLogEty generateGxqTworkFormLogEty(HashMap<String, Object> map) {
        GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
        SysUser user = (SysUser) map.get("user");
        etyLog.setWfId((Integer) map.get("wfId"));
        etyLog.setOpDeptNo(user.getDeptId().toString());
        etyLog.setOpDeptName(user.getDeptName());
        etyLog.setOpUserNo(user.getUserNo());
        etyLog.setOpUserName(user.getUserName());
        etyLog.setOpBtnId((Integer) map.get("opBtnId"));
        etyLog.setOpAction((String) map.get("opAction"));
        etyLog.setWsDesc((String) map.get("wsDesc"));
        if ("21".equals((String) map.get("sortCode"))) {
            etyLog.setWsOpen(0);
            etyLog.setEffectFlag(0);
        } else {
            etyLog.setWsOpen(1);
            etyLog.setEffectFlag(1);
        }
        etyLog.setOpDesc((String) map.get("opDesc"));
        return etyLog;
    }


    /**
     * 工单初稿修改页面
     *
     * @return
     */
    @GetMapping("/cmnFile/tWFormUpdate/{pkId}")
    public ModelAndView tWFormUpdate(@PathVariable Integer pkId, ModelMap map) {
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.getById(pkId);
        map.put("workFormBase", GxqTworkFormBaseEty);
        return new ModelAndView(prefix + "/cmnFile/tWFormUpdate");
    }

    /**
     * 工单初稿修改 提交数据
     */
    @PostMapping("cmnFile/tWFormUpdateJson")
    public AjaxResult tWFormUpdateJson(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        UpdateWrapper<GxqTworkFormBaseEty> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("DFmClaNo", GxqTworkFormBaseEty.getDfmClaNo())
                .set("DFmClaName", GxqTworkFormBaseEty.getDfmClaName())
                .set("DCntACode", GxqTworkFormBaseEty.getDcntAcode())
                .set("DCntAName", GxqTworkFormBaseEty.getDcntAname())
                .set("WsTopic", GxqTworkFormBaseEty.getWsTopic())
                .set("WfLimit", GxqTworkFormBaseEty.getWfLimit())
                .set("SetProID", GxqTworkFormBaseEty.getSetProId())
                .set("SetProName", GxqTworkFormBaseEty.getSetProName())
                .set("OpenX", GxqTworkFormBaseEty.getOpenX())
                .set("SpareInt", GxqTworkFormBaseEty.getSpareInt())
                .set("mark", GxqTworkFormBaseEty.getMark())
                .set("ReportFlag", GxqTworkFormBaseEty.getReportFlag())
                .set("WfRemark", GxqTworkFormBaseEty.getWfRemark())
                .eq("PkId", GxqTworkFormBaseEty.getPkId());
        boolean flag = gxqWorkFormBaseService.update(null, updateWrapper);
        if (flag) {
            SysUser user = ShiroUtils.getSysUser();
            GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
            etyLog.setWfId(GxqTworkFormBaseEty.getPkId());
            etyLog.setOpDeptNo(user.getDeptId().toString());
            etyLog.setOpDeptName(user.getDeptName());
            etyLog.setOpUserNo(user.getUserNo());
            etyLog.setOpUserName(user.getUserName());
            etyLog.setOpAction("修改");
            etyLog.setOpBtnId(-1);
            etyLog.setWsDesc("修改工单基本内容。");
            etyLog.setWsOpen(0);
            etyLog.setOpDesc("修改工单基本信息");
            etyLog.setEffectFlag(-1);
            gxqITworkFormLogService.save(etyLog);
        }
        return toAjax(flag);
    }

    /**
     * 进入重点诉求推荐列表页面
     *
     * @return
     */
    @GetMapping("/cmnFile/keyTwfRecommendList")
    public ModelAndView keyTwfRecommendList() {
        return new ModelAndView(prefix + "/cmnFile/keyTwfRecommendList");
    }

    /**
     * 加载重点诉求推荐列表数据
     *
     * @return
     */
    @PostMapping("/cmnFile/keyTwfRecommendListJson")
    public TableDataInfo keyTwfRecommendListJson(GxqTwfSendBackEty GxqTwfSendBackEty) {
        startPage();
        List<Map<String, Object>> list = gxqWorkFormBaseService.keyTwfRecommendListJson(GxqTwfSendBackEty);
        return getDataTable(list);
    }

    /**
     * 进入热点诉求推荐列表页面
     *
     * @return
     */
    @GetMapping("/cmnFile/hotSpotTwfRecommendList")
    public ModelAndView hotSpotTwfRecommendList(ModelMap map) {
        map.put("accSubjects", gxqWorkFormBaseService.hotSpotAccSubjects());
        return new ModelAndView(prefix + "/cmnFile/hotSpotTwfRecommendList");
    }

    /**
     * 加载热点诉求推荐列表数据
     *
     * @return
     */
    @PostMapping("/cmnFile/hotSpotRecommendListJson")
    public TableDataInfo hotSpotRecommendListJson(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        startPage();
        List<Map<String, Object>> list = gxqWorkFormBaseService.hotSpotRecommendListJson(GxqTworkFormBaseEty);
        return getDataTable(list);
    }

    /**
     * 进入近期热点事件推送列表页面
     */
    @GetMapping("/cmnFile/nearHotSpotTwfRecommendList")
    public ModelAndView nearHotSpotTwfRecommendList() {
        return new ModelAndView(prefix + "/cmnFile/nearHotSpotTwfRecommendList");
    }

    /**
     * 加载近期热点事件推送列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/cmnFile/nearHotSpotTwfRecommendListJson")
    public TableDataInfo nearHotSpotTwfRecommendListJson(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        startPage();
        List<Map<String, Object>> list = gxqWorkFormBaseService.nearHotSpotTwfRecommendListJson(GxqTworkFormBaseEty);
        return getDataTable(list);
    }

    /**
     * 进入近期典型事件推送列表页面
     */
    @GetMapping("/cmnFile/nearTypicalTwfRecommendList")
    public ModelAndView nearTypicalTwfRecommendList() {
        return new ModelAndView(prefix + "/cmnFile/nearTypicalTwfRecommendList");
    }

    /**
     * 加载近期典型事件推送列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/cmnFile/nearTypicalTwfRecommendListJson")
    public TableDataInfo nearTypicalTwfRecommendListJson(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        startPage();
        List<Map<String, Object>> list = gxqWorkFormBaseService.nearTypicalTwfRecommendListJson(GxqTworkFormBaseEty);
        return getDataTable(list);
    }

    /**
     * 进入特殊诉求列表页面
     *
     * @return 视图
     */
    @GetMapping("/cmnFile/specialTwfRecommendList")
    public ModelAndView specialTwfRecommendList() {
        return new ModelAndView(prefix + "/cmnFile/specialTwfRecommendList");
    }

    /**
     * 加载特殊诉求列表数据
     *
     * @param GxqTworkFormBaseEty 查询条件
     * @return 列表数据
     */
    @PostMapping("/cmnFile/specialTwfRecommendListJson")
    public TableDataInfo specialTwfRecommendListJson(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        startPage();
        List<Map<String, Object>> list = gxqWorkFormBaseService.specialTwfRecommendListJson(GxqTworkFormBaseEty);
        return getDataTable(list);
    }

    /**
     * 进入典型诉求列表页面
     *
     * @return 视图
     */
    @GetMapping("/cmnFile/typicalTwfRecommendList")
    public ModelAndView typicalTwfRecommendList() {
        return new ModelAndView(prefix + "/cmnFile/typicalTwfRecommendList");
    }


    /**
     * 加载典型诉求列表数据
     *
     * @param GxqTworkFormBaseEty 查询条件
     * @return 列表数据
     */
    @PostMapping("/cmnFile/typicalTwfRecommendListJson")
    public TableDataInfo typicalTwfRecommendListJson(GxqTworkFormBaseEty GxqTworkFormBaseEty) {
        startPage();
        List<Map<String, Object>> list = gxqWorkFormBaseService.typicalTwfRecommendListJson(GxqTworkFormBaseEty);
        return getDataTable(list);
    }

    /**
     * 进入上级不同意退回列表页面
     *
     * @return 视图
     */
    @GetMapping("/cmnFile/NotPass")
    public ModelAndView notPass() {
        return new ModelAndView(prefix + "/cmnFile/notPass");
    }


    /**
     * 加载上级不同意退回列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/cmnFile/NotPassListJson")
    public TableDataInfo notPassListJson(@RequestParam("workFormNo") String workFormNo, @RequestParam("sdeptName") String sdeptName) {
        startPage();
        List<Map<String, Object>> list = gxqWfSendBackService.notPassListJson(getSysUser().getDeptId(), workFormNo, sdeptName);
        return getDataTable(list);
    }

    /**
     * 检查二级部门下派的派单记录是否有下级回复
     * @param wfId 工单id
     * @param sdeptId 拟派部门编号
     * @return
     */
    @PostMapping("/checkSendBack")
    public Map<String,String> checkSendBack(Integer wfId,String sdeptId){
        String backDesc = "已回复";
        List<CheckSendBack> sendBack = gxqWfSendBackService.checkSendBack(wfId,sdeptId);

        for (CheckSendBack str : sendBack) {
            if ((str.getBackDesc() == null) || ("".equals(str.getBackDesc()))) {
                backDesc = "";
                break;
            }
        }
        if (sdeptId.length() > 6) {
            backDesc = "";
        }

        Map<String,String> result = new HashMap<>();
        result.put("backDesc", backDesc);
        return result;
    }

    /**
     * 下级办理结果页面
     */
    @GetMapping("/cmnFile/showSonBack/{wfId}")
    public ModelAndView showSonBack(@PathVariable("wfId") Integer wfId,Integer sendId,ModelMap map){
        List<GxqTwfSendBackEty> listSendDept = gxqWfSendBackService.listSendSubDeptByPid(sendId, getSysUser().getDeptId());
        List<GxqTwfSendBackEty> listSendDeptSon = gxqWfSendBackService.listSendDeptSon(wfId, getSysUser().getDeptId());
        if(listSendDept == null || listSendDept.size() == 0){
            listSendDept = listSendDeptSon;
        }else{
            if(listSendDept.size() != listSendDeptSon.size()){
                listSendDept = listSendDeptSon;
            }
        }
        List<GxqTwfSendBackEty[]> sonBackList = new ArrayList<>();
        if(!listSendDept.isEmpty()) {
            GxqTwfSendBackEty[] sons = new GxqTwfSendBackEty[3];
            for (int i = 0; i < listSendDept.size(); i++) {
                if (i % 3 == 0 && i != 0) {
                    sonBackList.add(sons);
                    sons = new GxqTwfSendBackEty[3];
                }
                sons[i % 3] = listSendDept.get(i);
                if ((i + 1) == listSendDept.size()) {
                    sonBackList.add(sons);
                }
            }
        }
        map.put("sonBackList",sonBackList);
        return new ModelAndView(prefix + "/cmnFile/showSonBack");
    }


    /**
     * 电话工单信息
     */
    @GetMapping("/cmnFile/showWorkFormInfo/{wfId}")
    public ModelAndView showWorkFormInfo(@PathVariable("wfId") Integer wfId,ModelMap map){
        SysUser user = getSysUser();
        map.put("user", user);
        GxqTworkFormBaseEty tWorkFormBase = gxqWorkFormBaseService.getById(wfId);
        List<Map<String, Object>> telExistTags = null;
        if(tWorkFormBase != null){
            commonController.hidetTel_gxq(user,tWorkFormBase);
            map.put("workForm",tWorkFormBase);
            // 查询工单已有标签
            telExistTags = tagService.selectTelExistTag(String.valueOf(tWorkFormBase.getGetDealPk()),user.getDeptId().toString(),user.getUserId().toString());
        }
        if(telExistTags != null && telExistTags.size() > 0){
            map.put("wfTag",telExistTags);
        }
        return new ModelAndView(prefix + "/cmnFile/showWorkFormInfo");
    }

    /**
     * 签收后直接回复工单页面
     */
    @GetMapping("/cmnFile/replyAfterSignS")
    public ModelAndView replyAfterSignS(Integer sendId,Integer wfId, ModelMap map) {
        map.put("sendId", sendId);
        map.put("sendBackEty", gxqWfSendBackService.getById(sendId));
        map.put("user", ShiroUtils.getSysUser());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("nowTime", sdf.format(DateUtils.getNowDate()));
        return new ModelAndView(prefix + "/cmnFile/replyAfterSignS");
    }

    /**
     * 签收后直接回复并审核工单页面
     */
    @GetMapping("/cmnFile/replyAfterAudit")
    public ModelAndView replyAfterAudit(Integer sendId,Integer wfId, ModelMap map) {
        map.put("sendId", sendId);
        map.put("sendBackEty", gxqWfSendBackService.getById(sendId));
        map.put("workForm",gxqWorkFormBaseService.getById(wfId));
        map.put("user", ShiroUtils.getSysUser());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("nowTime", sdf.format(DateUtils.getNowDate()));
        return new ModelAndView(prefix + "/cmnFile/replyAfterAudit");
    }


    /**
     * 批量审核回复页面
     * @param ids 派单ids
     */
    @GetMapping("/cmnFile/dlBatchChkReply")
    public ModelAndView dlBatchChkReply(String ids,ModelMap map){
        map.put("ids",ids);
        return new ModelAndView(prefix + "/cmnFile/dlBatchChkReply");
    }

    /**
     * 批量审核回复
     * @param ids 派单ids
     * @param act 通过与否
     */
    @PostMapping("/cmnFile/dlBatchChkReply")
    public AjaxResult dlBatchChkReplyJson(@RequestParam("ids") String ids,@RequestParam("act")int act,HttpServletRequest req){
        //保存影响结果
        AjaxResult res = AjaxResult.warn("操作失败");

        //被审核的派单id列表
        String[] idList = ids.split(",");
        SysUser user = getSysUser();

        for (String id : idList) {
            //派单对象
            GxqTwfSendBackEty sendBackEty = gxqWfSendBackService.getById(Integer.parseInt(id));
            if(act < 1) {
                sendBackEty.setBackState(12);
            }else{
                sendBackEty.setBackState(11);
            }
            //工单对象
            GxqTworkFormBaseEty workFormEty = gxqWorkFormBaseService.getById(sendBackEty.getWfId());
            if("210,240,260,290,310,340".contains(user.getRoles().get(0).getRoleId().toString())){
                if(Integer.parseInt(sendBackEty.getSortCode()) == 11){
                    workFormEty.setCrtStepId(810);
                    //普通工单
                    if(workFormEty.getSetProId() == 11){
                        workFormEty.setCrtStepName("待审定结果");
                    }else {//市级
                        workFormEty.setCrtStepName("待效能办审查");
                    }
                    if (act < 1) {
                        workFormEty.setCrtStepId(801);
                        workFormEty.setCrtStepName("市级不通过");
                    }
                }
            }
            //调用审核单个派单回复的方法
            res = dlChkReply(sendBackEty,workFormEty,req);
        }
        return res;
    }

    /**
     * @Description: 修改回复
     * @Param: [sendBackEty]
     * @return: AjaxResult
     */
    @PostMapping("/btnReplyClick")
    public AjaxResult btnReplyClick(GxqTwfSendBackEty sendBackEty){
        SysUser sysUser = getSysUser();
        String oldStr = sendBackEty.getBackDesc();
        if(oldStr != null){
            sendBackEty.setBackDesc(oldStr.replace(removeStr,""));
        }
        UpdateWrapper<GxqTwfSendBackEty> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("Reason",sendBackEty.getReason())
                .set("BUserName", sysUser.getUserName())
                .set("BDutyDept", sysUser.getDeptName())
                .set("BDutyTel", sysUser.getNoTel())
                .set("BackTime",new Date())
                .set("BackDesc",sendBackEty.getBackDesc())
                .set("BackRemark",sendBackEty.getBackRemark())
                .eq("PkId",sendBackEty.getPkId());
        boolean flag = gxqWfSendBackService.update(null, updateWrapper);
        //记录日志
        if(flag){
            SysUser user = ShiroUtils.getSysUser();
            GxqTworkFormLogEty twfLog = sendBackEty.getTworkFormLogEty();
            twfLog.setWfId(sendBackEty.getWfId());
            twfLog.setOpDeptNo(user.getDeptId().toString());
            twfLog.setOpDeptName(user.getDeptName());
            twfLog.setOpUserNo(user.getUserNo());
            twfLog.setOpUserName(user.getUserName());
            twfLog.setOpAction("修改");
            twfLog.setOpBtnId(-1);
            twfLog.setWsDesc("修改回复信息");
            twfLog.setWsOpen(0);
            twfLog.setOpDesc("修改回复内容");
            twfLog.setEffectFlag(-1);
            gxqITworkFormLogService.save(twfLog);
        }
        return toAjax(flag);
    }

    /**
     * 2023/1/10
     * 派单部门编辑页面
     */
    @GetMapping("/sendDeptEditPart/{sbid}")
    public ModelAndView sendDeptEditPart(@PathVariable Integer sbid, ModelMap map) {
        GxqTwfSendBackEty twfSendBack = gxqWfSendBackService.selectByPkId(sbid);
        map.put("twfSendBack", twfSendBack);
        GxqTworkFormBaseEty tworkFormBase = gxqWorkFormBaseService.getById(twfSendBack.getWfId());
        map.put("tworkFormBase", tworkFormBase);
        return new ModelAndView(prefix1 + "/sendDeptEditPart");
    }

    /**
     * 进入已删除电话工单页面
     * @return
     */
    @GetMapping("/cmnFile/deletedTelWF")
    public ModelAndView deletedTelWF(){
        return new ModelAndView(prefix+"/cmnFile/deletedTelWF");
    }

    /**
     * 市级已删除电话工单
     */
    @PostMapping("/cmnFile/deletedTelWFJson")
    public TableDataInfo deletedTelWFJson(GxqTwfSendBackEty GxqTwfSendBackEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String,Object>> list = gxqWfSendBackService.deleteTelWFJson(GxqTwfSendBackEty, user.getDeptId().toString());
        return getDataTable(list);
    }

    /**
     * 同源派单搜索页面
     * @return
     */
    @GetMapping("/cmnFile/searchForm")
    public ModelAndView searchForm(){
        return new ModelAndView(prefix+"/cmnFile/searchForm");
    }

    /**
     * 同源转派搜索页面
     * @return
     */
    @GetMapping("/cmnFile/searchReplyForm")
    public ModelAndView searchReplyForm(){
        return new ModelAndView(prefix+"/cmnFile/searchReplyForm");
    }

    /**
     * 未签收列表数据--同源派单展示
     */
    @PostMapping("/cmnFile/notSignList")
    public TableDataInfo notSignList(GxqTwfSendBackEty GxqTwfSendBackEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<GxqTwfSendBackEty> list = gxqWfSendBackService.notSignList(GxqTwfSendBackEty, user.getDeptId().toString());
        return getDataTable(list);
    }

    /**
     * 未签收列表数据--同源派单展示
     */
    @PostMapping("/cmnFile/waitReplyList")
    public TableDataInfo waitReplyList(GxqTwfSendBackEty GxqTwfSendBackEty) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = gxqWfSendBackService.waitReplyListJson(GxqTwfSendBackEty,user.getDeptId().toString());
        return getDataTable(list);
    }

    /**
     * 批量派单页面
     * @return
     */
    @GetMapping("/cmnFile/batchSendToSon")
    public ModelAndView batchSendToSon(Integer times, ModelMap map){
        SysUser user = getSysUser();
        map.put("deptId", user.getDeptId());
        map.put("sdeptId",user.getDeptId());//派发单位
        map.put("sdeptName",user.getDeptName());//派发单位
        map.put("suserNo",user.getUserNo());//派发人
        map.put("suserName",user.getUserName());//派发人
        if(times != null && times == 1){
            map.put("seqCode",10);
            map.put("seqName","初次交办");
        }else if(times != null && times == 2){
            map.put("seqCode",20);
            map.put("seqName","再次交办");
        }
        return new ModelAndView(prefix+"/cmnFile/batchSendToSon");
    }

    /**
     * 进入未处理电话工单页面
     */
    @GetMapping("/cmnFile/notHandleTelList")
    public ModelAndView notHandleTelList() {
        return new ModelAndView(prefix + "/cmnFile/notHandleTelList");
    }

    /**
     * 加载未处理电话工单数据
     */
    @PostMapping("/cmnFile/notHandleTelJson")
    public TableDataInfo notHandleTelJson(GxqTwfSendBackEty GxqTwfSendBackEty) {
        startPage();
        List<Map<String, Object>> list;
        if (IS_GAOXIN()){
            list = gxqWfSendBackService.notHandleTelJson_gxq(getSysUser().getDeptId().toString(),GxqTwfSendBackEty);
        }else{
            list = gxqWfSendBackService.notHandleTelJson(getSysUser().getDeptId().toString(),GxqTwfSendBackEty);
        }
        return getDataTable(list);
    }

    /**
     * 导出未处理电话工单数据
     */
    @PostMapping("/cmnFile/notHandleTelExport")
    @ResponseBody
    public AjaxResult notHandleTelExport() {
        SysUser user = getSysUser();
        List<NotHandleTelExcel> list = gxqWfSendBackService.notHandleTelExport(user.getDeptId().toString());
        ExcelUtil<NotHandleTelExcel> util = new ExcelUtil<>(NotHandleTelExcel.class);
        return util.exportExcel(list, "未处理电话工单数据");
    }

    /**
     * 进入上级催办电话工单页面
     */
    @GetMapping("/cmnFile/supUrgeTelOrder")
    public ModelAndView supUrgeTelOrder() {
        return new ModelAndView(prefix + "/cmnFile/supUrgeTelOrder");
    }

    /**
     * 加载上级催办电话工单数据
     */
    @PostMapping("/cmnFile/supUrgeTelOrderJson")
    public TableDataInfo supUrgeTelOrderJson(GxqTwfSendBackEty GxqTwfSendBackEty) {
        startPage();
        List<Map<String, Object>> list = gxqWfSendBackService.supUrgeTelOrderJson(getSysUser().getDeptId().toString(),GxqTwfSendBackEty);
        return getDataTable(list);
    }

    /**
     * 导出上级催办电话工单数据
     */
    @PostMapping("/cmnFile/supUrgeTelOrderExport")
    @ResponseBody
    public AjaxResult supUrgeTelOrderExport() {
        SysUser user = getSysUser();
        List<SupUrgeTelExcel> list = gxqWfSendBackService.supUrgeTelOrderExport(user.getDeptId().toString());
        ExcelUtil<SupUrgeTelExcel> util = new ExcelUtil<>(SupUrgeTelExcel.class);
        return util.exportExcel(list, "上级催办电话工单数据");
    }

    /**
     * 抄送部门选择
     *
     * @return
     */
    @GetMapping("/cmnFile/copyDeptAll")
    public ModelAndView copyDeptAll(ModelMap map) {
        map.put("areaNo", getSysUser().getAreaNo());
        map.put("deptId", getSysUser().getDeptId());
        return new ModelAndView(prefix + "/cmnFile/copyDeptAll");
    }

    /**
     * 审核本级回复 未处理列表页面
     *
     * @return
     */
    @GetMapping("/cmnFile/getPeerReplyOverUntreated")
    public ModelAndView getPeerReplyOverUntreated(ModelMap map) {
        map.put("num", gxqWfSendBackService.countMuReplyChkJson(getSysUser().getDeptId()));
        return new ModelAndView(prefix + "/cmnFile/getPeerReplyOverUntreated");
    }

    /**
     * 审核本级回复 未处理列表获取页面
     *
     * @return
     */
    @GetMapping("/cmnFile/peerReplyOverUntreated")
    public ModelAndView peerReplyOverUntreated() {
        return new ModelAndView(prefix + "/cmnFile/peerReplyOverUntreated");
    }

    /**
     * 审核本级回复 已处理列表页面
     *
     * @return
     */
    @GetMapping("/cmnFile/peerReplyOverTreated")
    public ModelAndView peerReplyOverTreated() {
        return new ModelAndView(prefix + "/cmnFile/peerReplyOverTreated");
    }

    /**
     * 获取审核本级回复的列表数据
     */
    @PostMapping("/cmnFile/getMuReplyChkJson")
    public TableDataInfo getMuReplyChkJson(QueryConditionVo queryConditionVo) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = gxqWfSendBackService.getMuReplyChkJson(user.getUserId(), user.getDeptId(), queryConditionVo);
        return getDataTable(list);
    }

    /**
     * 加载审核本级回复的列表数据
     */
    @PostMapping("/cmnFile/muReplyChkJsonData")
    public TableDataInfo muReplyChkJsonData(QueryConditionVo queryConditionVo) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = gxqWfSendBackService.muReplyChkJsonData(user.getDeptId(), queryConditionVo);
        return getDataTable(list);
    }

    /**
     * 加载本部门已办毕列表数据
     */
    @PostMapping("/cmnFile/muReplyOverJsonData")
    public TableDataInfo muReplyOverJsonData(QueryConditionVo queryConditionVo) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list = gxqWfSendBackService.muReplyOverJsonData(user.getDeptId(),queryConditionVo);
        return getDataTable(list);
    }

    /**
     * 审核本级回复 未处理列表页面
     *
     * @return
     */
    @GetMapping("/cmnFile/backPeerDeptAuditT")
    public ModelAndView backPeerDeptUndisposedT() {
        return new ModelAndView(prefix + "/cmnFile/backPeerDeptAuditT");
    }

    /**
     * 加载(区县)退回的工单T列表数据
     */
    @PostMapping("/cmnFile/backPeerDeptAuditTJson")
    public TableDataInfo backPeerDeptAuditTJson(QueryConditionVo queryConditionVo) {
        startPage();
        SysUser user = ShiroUtils.getSysUser();
        List<Map<String, Object>> list;
        if (IS_GAOXIN()){
            list = gxqWfSendBackService.backPeerDeptAuditTJson_gxq(user.getDeptId(), queryConditionVo);
        }else{
            list = gxqWfSendBackService.backPeerDeptAuditTJson(user.getDeptId(), queryConditionVo);
        }
        return getDataTable(list);
    }

    /**
     * 退回本单位电话工单审核 退回审核页面
     *
     * @param
     * @return
     */
    @GetMapping("/cmnFile/auditPeerBackT/{pkId}")
    public ModelAndView auditPeerBackT(@PathVariable("pkId") Integer pkId, ModelMap map) {
        GxqTwfSendBackEty GxqTwfSendBackEty = gxqWfSendBackService.getById(pkId);
        GxqTworkFormBaseEty GxqTworkFormBaseEty = gxqWorkFormBaseService.getById(GxqTwfSendBackEty.getWfId());
        if (GxqTworkFormBaseEty !=null ) {
            map.put("workFormBase", GxqTworkFormBaseEty);
            map.put("sendBackEty",GxqTwfSendBackEty);
        }
        map.put("pkId",pkId);
        //驳回短信通知
        // map.put("smsInform",smsNoteService.selectSmsInform(getSysUser().getDeptId().toString(), getSysUser().getUserId().toString()));
        return new ModelAndView(prefix + "/cmnFile/auditPeerBackT");
    }

    /**
     * 退回本单位电话工单审核 退回审核数据
     *
     * @return
     */
    @PostMapping("/cmnFile/auditPeerBackTJson")
    public AjaxResult auditPeerBackTJson(GxqTwfSendBackEty GxqTwfSendBackEty, HttpServletRequest req) {
        boolean resultFlag; String opAction = "";
        String reason = req.getParameter("txtReason");
        SysUser user = ShiroUtils.getSysUser();
        int backState = GxqTwfSendBackEty.getBackState();
        UpdateWrapper<GxqTwfSendBackEty> updateWrapper = new UpdateWrapper<>();
        if (backState == 51){// 本级通过
            opAction = "审核通过";
            updateWrapper.set("SendState", 40)
                    .set("FlagInt", 1)
                    .set("GetTime", new Date())
                    .set("BackState", 11)
                    .set("GetPerson", user.getUserNo())
                    .set("GetPerTel", user.getPhonenumber())
                    .set("BackRemark", GxqTwfSendBackEty.getBackRemark())
                    .set("SendRemark", "")
                    .eq("PkId", GxqTwfSendBackEty.getPkId());
        }else if (backState == 60){// 本级不通过
            opAction = "审核不通过";
            if (reason.length() > 1){
                reason = GxqTwfSendBackEty.getSendRemark()+"（"+reason+"）";
            }else {
                reason = GxqTwfSendBackEty.getSendRemark();
            }
            updateWrapper.set("SendState", backState)
                    .set("FlagInt", 0)//审核通过
                    .set("GetTime", new Date())
                    .set("BackState", 12)
                    .set("GetPerson", user.getUserNo())
                    .set("GetPerTel", user.getPhonenumber())
                    .set("BackRemark", GxqTwfSendBackEty.getBackRemark())
                    .set("SendRemark", reason)
                    .eq("PkId", GxqTwfSendBackEty.getPkId());
        }
        resultFlag = gxqWfSendBackService.update(updateWrapper);
        if (resultFlag) {
            GxqTworkFormLogEty etyLog = new GxqTworkFormLogEty();
            etyLog.setWfId(GxqTwfSendBackEty.getWfId());
            etyLog.setOpDeptNo(user.getDeptId().toString());
            etyLog.setOpDeptName(user.getDeptName());
            etyLog.setOpUserNo(user.getUserNo());
            etyLog.setOpUserName(user.getUserName());
            etyLog.setOpBtnId(-1);
            etyLog.setOpAction("退回本级" + opAction);
            etyLog.setWsDesc("退回本级审核工单：" + opAction);
            if ("21".equals(GxqTwfSendBackEty.getSortCode())) {
                etyLog.setWsOpen(0);
                etyLog.setEffectFlag(0);
            } else {
                etyLog.setWsOpen(1);
                etyLog.setEffectFlag(1);
            }
            etyLog.setOpDesc("退回本级审核: " + user.getDeptName() + "（" + opAction + "）");
            resultFlag = gxqITworkFormLogService.save(etyLog) > 0;
        }
        //更新父级派单状态
        commonDao.proTsubBackState(GxqTwfSendBackEty.getWfId(),GxqTwfSendBackEty.getPkId());
        return toAjax(resultFlag);
    }

    /**
     * 进入本级不同意退回列表页面
     *
     * @return 视图
     */
    @GetMapping("/cmnFile/peerDeptNotPass")
    public ModelAndView peerDeptNotPass() {
        return new ModelAndView(prefix + "/cmnFile/peerDeptNotPass");
    }

    /**
     * 加载上级不同意退回列表数据
     *
     * @return 列表数据
     */
    @PostMapping("/cmnFile/peerDeptNotPassListJson")
    public TableDataInfo peerDeptNotPassListJson(QueryConditionVo queryConditionVo) {
        startPage();
        List<Map<String, Object>> list = gxqWfSendBackService.peerDeptNotPassListJson(getSysUser().getDeptId(), queryConditionVo);
        return getDataTable(list);
    }

    /**
     * 进入获取审核本级回复未处理的工单配置页面
     *
     * @return 视图
     */
    @GetMapping("/cmnFile/countAWaitPeerReply")
    public ModelAndView countAWaitPeerReply(ModelMap map) {
        map.put("num", gxqWfSendBackService.countMuReplyChkJson(getSysUser().getDeptId()));
        return new ModelAndView(prefix + "/cmnFile/countAWaitPeerReplyT");
    }

    /**
     * 随机获取审核本级回复未处理工单数
     * @return 视图
     */
    @PostMapping("/cmnFile/getAWaitPeerReplyTCount")
    public AjaxResult getAWaitPeerReplyTCount() {
        return AjaxResult.success("获取成功",gxqWfSendBackService.countMuReplyChkJson(getSysUser().getDeptId()));
    }

    /**
     * 随机获取N条审核本级回复工单 并保存到中间表中
     */
    @PostMapping("/cmnFile/getAWaitPeerReplyList")
    @RequestBody
    public Object getAWaitPeerReplyList(Integer num) {
        SysUser user = ShiroUtils.getSysUser();
        List<GxqTwfSendBackEty> list = new ArrayList<>();
        AjaxResult ajaxResult = new AjaxResult();
        try {
            list = gxqWfSendBackService.getAWaitPeerReplyList(num, user.getDeptId().toString());
            for (GxqTwfSendBackEty twfSendBack : list) {
                GetAWaitReplyTelEty telEty = new GetAWaitReplyTelEty();
                telEty.setSbId(twfSendBack.getPkId());
                telEty.setTelPkId(twfSendBack.getWfId());
                telEty.setUserNo(user.getUserNo());
                telEty.setUserName(user.getUserName());
                telEty.setDeptNo(user.getDeptId().toString());
                telEty.setDeptName(user.getDeptName());
                telEty.setCreateTime(new Date());
                if (getAWaitReplyTelService.checkBySbId(telEty.getSbId())){
                    getAWaitReplyTelService.save(telEty);
                }else{
                    ajaxResult.put("msg", "工单已被获取办理中，请刷新页面重试！");
                    return ajaxResult;
                }
            }
            ajaxResult.put("msg", "已成功获取到" + list.size() + "条数据");
        } catch (Exception e) {
            e.printStackTrace();
        }
        ajaxResult.put("code", 0);
        return ajaxResult;
    }
}
